<%@ page contentType="text/html; charset=utf-8" language="java"  %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<%@include file="/static/common/header.jsp"%>
		<%@include file="/static/common/easyui.jsp"%>
		<script src="${ctx}/static/jquery/jquery.form.js" type="text/javascript"></script>
		<link href="${ctx}/static/css/main.css" rel="stylesheet">
		<script>
		
		var options = { 
			 	beforeSubmit: progressing,
		        contentType: "application/x-www-form-urlencoded;charset=utf-8", 
				dataType:"json",  
		        success: function(jsonStr){	
					 progressed();			 
					if(jsonStr.STATE=='ERROR'){
							showInfo("操作失败:"+jsonStr.INFO);
					}else{
						$('#dicEditWin').window('close');	
						reloadTreeGridData();
						showInfo();
					}
				},
				resetForm:true
		    };
		
		
		
		//删除
		function delDicBut(dicId) {
			$.messager.confirm('确认', '确认删除所选?', function(r){ if (r){
				$.ajax({
					type:"post", 
					cache: false, 
					dataType:"json", 
					async:false,
					url: "${ctx}/common/dic/del",
					data: "id="+dicId,
					beforeSend:function(){ progressing()}, 
					complete:function(){progressed()},
					success:function(jsonStr){
						if(jsonStr.STATE=='ERROR'){
							showInfo("操作失败:"+jsonStr.INFO);
						}else{
							reloadTreeGridData();
							showInfo();
						}
					}
				});
			}});
		}
		
		//获取字典ID
		function getSelectedId(){
			var row = $('#dicTreeGrid').datagrid('getSelected');
			if (row){
				return row.id;
			}else{
				return "";
			}
		}
		
		//保存字典信息
		function saveDataInfo(){
			var isValid = $("#dicEditForm").form('validate');
			
			if($("#id").val()!="" && $("#upDicId").val()==$("#id").val()){
				isValid=false;
				showInfoCenter("请选择上级字典");
			}
			
			if (!isValid){
				$.messager.progress('close');
			}
			if(isValid){
				$('#dicEditForm').ajaxSubmit(options);
			}else{
				$.messager.progress('close');
			}
			
		}
		
		//跳转新增或修改页面
		function modify(OPT){
		 	$('#dicEditForm').resetForm();
		 	//初始化设置
			$("#id").val("");
			$("#dicLevel").val("");
			$("#upDicId").val("");
			$("#upDicName").val("");
			
			 var mId=getSelectedId();
			 if(OPT=='N'){
			 	mId="";
			 }else if(mId==""){
			 	showInfoCenter("请选择要修改的数据！");
				return;
			 }
			 $('#dicEditWin').window('open');
			 
			 if(mId!=''){
				 $.ajax({
					type:"post",
					cache: false,
					dataType:"json",
					async:false,
					url: "${ctx}/common/dic/detail",
					data: "id="+mId,
					beforeSend:function(){ progressing()}, 
					complete:function(){progressed()},
					success:function(jsonStr){
						if(jsonStr.STATE=='ERROR'){
							showInfo("操作失败:"+jsonStr.INFO);
						}else{
							$("#dicEditForm").form("load",jsonStr);
						}
					}
				});
			}
		 }
		
		//打开上级字典选择窗口
		function setUpDicInfo(){
			$('#dicTreeGridData').treegrid('reload');
			$('#dicSelectWin').window('open');
		}
		
		//设置上级字典信息
		function setNodeInfo(node){
			$("#upDicId").val(node.id);
			$("#upDicName").val(node.dicName);
			var upLevel = node.dicLevel;
			$("#dicLevel").val(upLevel+1);
			$('#dicSelectWin').window('close');
		}
		
		//重新加载数据
		function reloadTreeGridData(id){
			mId=getSelectedId();
			if(mId!=""){
				var pnode= $('#dicTreeGrid').treegrid('getParent', mId);
				if(pnode) mId=pnode.id;else mId="";
			}
			
			if(mId==""){
				$('#dicTreeGrid').treegrid('reload');
			}else{
				$('#dicTreeGrid').treegrid('reload', pnode.id);
			}
			$('#dicTreeGrid').treegrid('unselectAll');
		}
		
		$(document).ready(function () {  
            $("#dicTreeGrid").treegrid({  
                title: '系统字典',
                url: '${ctx}/common/dic/treeGridData',
                idField: 'id',
                treeField: 'dicName',
                fit:true,
                animate:true,
                columns: [[
                    { field: 'dicName', title: '字典名称', width: 300 },
                    { field: 'dicValue', title: '字典编码', width: 200 },
                    { field: 'dicValue2', title: '字典编码2', width: 200 },
                    { field: 'dicValue3', title: '字典编码3', width: 200 },
                    {field:'id',title:'操作',width:100,rowspan:2,formatter:function(value, row, index){
						return '<shiro:hasPermission name="____AgentAdmin">' + 
						'<a href="#" class="removecls" onClick="delDicBut(\''+row.id+'\')">[ 删除 ]</a>' + 
						'</shiro:hasPermission>'; 
					}}
                ]],
                rownumbers:true,
        		
        		toolbar:'#tb',
        		onLoadSuccess:function(){
        			$('.removecls').linkbutton({text:'删除',plain:true,iconCls:'icon-remove'}); 
        			$('#dicTreeGrid').treegrid('clearSelections'); //一定要加上这一句，要不然datagrid会记住之前的选择状态，删除时会出问题
        		}
            })  
        })
		</script>
	</head>
	
	<body id="orgLayOut" class="easyui-layout">
		<div data-options="region:'center',collapsed:false,border:false" class="bmargin">
				<table id="dicTreeGrid" class="easyui-treegrid" >
				</table>
		</div>
		
		<div id="tb" class="datagrid-toolbar" style="padding:5px;height:auto">
			<div>
				<shiro:hasPermission name="____AgentAdmin">
            		<a href="javascript:void(0)" onClick="modify('N')" id="btnadd" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
            		<a href="javascript:void(0)" onClick="modify('E')" id="btnedit" class="easyui-linkbutton" iconCls="icon-edit" plain="true">更新</a>
            		<a href="javascript:void(0)" onClick="reloadTreeGridData();" id="btnedit" class="easyui-linkbutton" iconCls="icon-reload" plain="true">刷新</a>
        		</shiro:hasPermission>
        	</div>
		</div>
		
		<div id="dicEditWin" class="easyui-window" title="Modal Window" data-options="modal:true,closed:true,iconCls:'icon-edit',title:'字典维护'" style="width:800px;height:400px;padding:10px;">
			<form method="post" id="dicEditForm" action="${ctx}/common/dic/updateDic">
				<input type="hidden" name="id" id="id" value=""/>
				<table  class="listTable" align="center" width="95%">
      				<tr height="30">
      					<td align="left"> 上级字典： </td>
      					<td align="left">
      						<input type="hidden" name="upDicId" id="upDicId" value="">
      						<input type="hidden" name="dicLevel" id="dicLevel" value="">
      						<input type="text" class="easyui-validatebox" name="upDicName" id="upDicName" readonly="true" onClick="setUpDicInfo()">
      						<a class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" href="javascript:void(0);" onClick="setUpDicInfo()">选取</a>
      					</td>
      				</tr>
      				<tr height="30">
      					<td align="left"> 字典名称： </td>
      					<td align="left">
      				  		<input type="text" id="dicName" name="dicName" value="" class="easyui-validatebox" data-options="required:true"/>
      					</td>
      				</tr>
      				<tr height="30">
      					<td align="left"> 字典编码： </td>
      					<td align="left">
      				  		<input type="text" id="dicValue" name="dicValue" value="" class="easyui-validatebox" data-options="required:true"/>
      					</td>
      				</tr>
      				<tr height="30">
      					<td align="left"> 字典编码2： </td>
      					<td align="left">
      				  		<input type="text" id="dicValue2" name="dicValue2" value="" class="easyui-validatebox" />
      					</td>
      				</tr>
      				<tr height="30">
      					<td align="left"> 字典编码3： </td>
      					<td align="left">
      				  		<input type="text" id="dicValue3" name="dicValue3" value="" class="easyui-validatebox" />
      					</td>
      				</tr>
      				<tr height="30">
      					<td align="left"> 状态： </td>
      					<td align="left">
      				  		<select id="used" name="used">
								<option value="1" >启用</option>
								<option value="0" >禁用</option>
							</select>
      					</td>
      				</tr>
      				<tr height="30">
      					<td align="left"> 排序： </td>
      					<td align="left">
      				  		<input class="easyui-validatebox" type="text" name="sort" id="sort" value="100" />
      					</td>
      				</tr>
      				<tr height="50">
      					<td colspan="4" align="center">
      				  		<a href="javascript:void(0);" id="btn-back"
								onclick="$('#dicEditWin').window('close');" class="easyui-linkbutton" iconCls="icon-back">返回</a> 
							<a href="javascript:void(0);" id="btn-add"
								onclick="saveDataInfo()" class="easyui-linkbutton" iconCls="icon-save">保存</a> 
						</td>
      				</tr>
				</table>
			</form>
		</div>
		
		<div id="dicSelectWin" class="easyui-window" title="Modal Window" data-options="modal:true,closed:true,iconCls:'icon-edit',title:'<font color=red >选取上级字典</font><font color=blue >（双击选取）</font>'" style="width:580px;height:320px;padding:10px;">
    		<table id="dicTreeGridData" class="easyui-treegrid"  
			data-options="
				url: '${ctx}/common/dic/treeGridData',
				rownumbers: true,
				idField: 'id',
				treeField: 'dicName',
				fit:true,
				animate:true,
				onDblClickRow:function(row){setNodeInfo(row);}
				">
    			<thead>
      				<tr>
						<th data-options="field:'dicName'" width="300">字典名称</th>
						<th data-options="field:'dicValue'" width="180">字典编码</th>
      				</tr>
    			</thead>
  			</table>
		</div>
		
	</body>
</html>